#include <iostream>
#include <vector>
#include <algorithm>
#include <unicode/ustdio.h>
#include <unicode/uchar.h>

enum eSplitterState {
    eInWord = 0,
    eNotInWord
};


int main( int argc,char** argv)
{
    if (argc < 4) {
        std::cerr << argv[0] << " \"wiki file\" \"alphabet file\" \"output file\"" << std::endl;
        return 1; 
    }

    typedef std::vector<UChar32>    TAlpahabet;

    UFILE*  alphabetFile(u_fopen(argv[2], "r", 0, "UTF-8"));

    if (0 == alphabetFile) {
        std::cerr << "could not open " << argv[1] << " for read" << std::endl;
        return 1;
    }

    TAlpahabet  alphabet;

    while (not u_feof(alphabetFile)) {
        UChar32 alphaChar(u_fgetcx(alphabetFile));

        if (u_isalpha(alphaChar)) {
            alphabet.push_back(alphaChar);
        }
    }

    std::sort(alphabet.begin(), alphabet.end());

    for (unsigned int i = 0; i != alphabet.size(); ++i) {
        std::cout << alphabet[i] << std::endl;
    }

    u_fclose(alphabetFile);


    eSplitterState  state(eNotInWord);

    UFILE*  wikiFile(u_fopen(argv[1], "r", 0, "UTF-8"));

    if (0 == wikiFile) {
        std::cerr << "could not open " << argv[1] << " for read" << std::endl;
        return 1;
    }

    UFILE*  wordListFile(u_fopen(argv[3], "w", 0, "UTF-8"));

    if (0 == wordListFile) {
        std::cerr << "could not open " << argv[2] << " for write" << std::endl;
        return 1;
    }

    while (not u_feof(wikiFile)) {
        UChar32 nextChar(u_fgetcx(wikiFile));

        nextChar    =   u_tolower(nextChar);
        
        UBool   isNextCharAlpha(FALSE);

        TAlpahabet::const_iterator  alphaPlace(std::lower_bound( alphabet.begin()
                                                               , alphabet.end()
                                                               , nextChar));

        if ((alphabet.end() != alphaPlace) and (*alphaPlace == nextChar)) {
            isNextCharAlpha =   TRUE;
        }

        if ((eInWord == state) and (TRUE == isNextCharAlpha)) {
            u_fputc(nextChar, wordListFile);
        }

        if ((eInWord == state) and (FALSE == isNextCharAlpha)) {
            state   =   eNotInWord;
            u_fprintf(wordListFile, "\n");
        }

        if ((eNotInWord == state) and (TRUE == isNextCharAlpha)) {
            u_fputc(nextChar, wordListFile);
            state   =   eInWord;
        }

        if ((eNotInWord == state) and (FALSE == isNextCharAlpha)) {
            //nothing to do
        }

        
    }
    
    u_fclose(wordListFile);
    u_fclose(wikiFile);
    
    return 0;
}
